package com.leyou.personnel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leyou.common.pojo.Result;
import com.leyou.common.pojo.ResultLogin;
import com.leyou.common.pojo.UserLogin;
import com.leyou.common.utils.StringUtils;
import com.leyou.common.utils.token.JwtUtils;
import com.leyou.common.utils.token.RasUtils;
import com.leyou.personnel.data.dto.AdministratorsManagerDto;
import com.leyou.personnel.data.entity.AdministratorsManager;
import com.leyou.personnel.data.vo.ManagerListSearchVo;
import com.leyou.personnel.data.vo.ManagerLoginVo;
import com.leyou.personnel.mapper.AdministratorsAuthorityMapper;
import com.leyou.personnel.mapper.AdministratorsManagerMapper;
import com.leyou.personnel.service.AdministratorsManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xhh
 * @since 2022-05-18
 */
@Service
public class AdministratorsManagerServiceImpl extends ServiceImpl<AdministratorsManagerMapper, AdministratorsManager> implements AdministratorsManagerService {
    @Autowired
    private AdministratorsManagerMapper administratorsManagerMapper;
    @Autowired
    private AdministratorsAuthorityMapper authorityMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public ResultLogin managerLogin(ManagerLoginVo managerLoginVo) {
        QueryWrapper wrapper=new QueryWrapper();
        ResultLogin result=new ResultLogin();
        String token=null;

        wrapper.clear();
        wrapper.eq("manager_account_number",managerLoginVo.getManagerAccountNumber());
        wrapper.eq("manager_password",managerLoginVo.getManagerPassword());


        AdministratorsManager administratorsManager = administratorsManagerMapper.selectOne(wrapper);
        //登录成功
        if(administratorsManager!=null) {
            List<String> listByManagerNumber = authorityMapper.getListByManagerNumber(administratorsManager.getRoleNumber());
            //将管理员编号 放入session 并将session为键权限编号为值的数据存入redis。方便权限认证
            String managerAccountNumber = administratorsManager.getManagerAccountNumber();
            SetOperations zSetOperations = redisTemplate.opsForSet();
            zSetOperations.members(managerAccountNumber);
            for(String number:listByManagerNumber){
                zSetOperations.add(managerAccountNumber,number);
            }
            //将userLogin 加密生成token传递给前端
            result.setCode("200");
            try {
                UserLogin userLogin=new UserLogin();
                userLogin.setUserName(administratorsManager.getManagerName());
                userLogin.setManagerAccountNumber(administratorsManager.getManagerAccountNumber());
                userLogin.setRoleNumber(administratorsManager.getRoleNumber());
                token = JwtUtils.generateToken(userLogin, 60*24*7, RasUtils.getPrivateKey(RasUtils.privateKeyPath));
            } catch (Exception e) {
                e.printStackTrace();
            }
            result.setResult(Result.RESULT_FLG.SUCCESS.getValue());
            result.setData(administratorsManager);
            result.setMsg("登录成功");
            result.setToken(token);
        }else {
            result.setCode("404");
            result.setResult(Result.RESULT_FLG.FAIL.getValue());
            result.setData(null);
            result.setMsg("账号或密码错误");
        }
        return result;
    }

    @Override
    public Result managerList(ManagerListSearchVo vo) {
        Result<IPage<AdministratorsManagerDto>> result=new Result();
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("deleted","0");
        if(vo.getManagerAccountNumber()!=null && !vo.getManagerAccountNumber().equals("")) {
            wrapper.eq("manager_account_number", vo.getManagerAccountNumber());
        }
        if(vo.getManagerName()!=null && !vo.getManagerName().equals("") ) {
            wrapper.eq("manager_name", vo.getManagerName());
        }
        Page page=new Page(vo.getPage(),vo.getLimit());
        IPage<AdministratorsManagerDto> administratorsManagerDtoIPage = administratorsManagerMapper.managerList(page,wrapper);
        if(administratorsManagerDtoIPage.getRecords()!=null) {
            result.setResult(Result.RESULT_FLG.SUCCESS.getValue());
            result.setData(administratorsManagerDtoIPage);
            result.setCode("200");
            result.setMsg("获取成功");
        }else{
            result.setCode("403");
            result.setResult(Result.RESULT_FLG.FAIL.getValue());
            result.setData(null);
            result.setMsg("没有对应的用户");
        }
        return result;
    }

    @Override
    public Result managerInsert(AdministratorsManager administratorsManager) {
        Result result=new Result();
        int insert = administratorsManagerMapper.insert(administratorsManager);
        if(insert>0){
            result.setCode("200");
            result.setResult(Result.RESULT_FLG.SUCCESS.getValue());
            result.setData(null);
            result.setMsg("添加成功");
        }else{
            result.setCode("403");
            result.setResult(Result.RESULT_FLG.FAIL.getValue());
            result.setData(null);
            result.setMsg("添加失败");
        }
        return result;
    }

    @Override
    public Result managerDel(List<String> managerAccountNumbers) {
        Result<List<String>> result=new Result();
        QueryWrapper queryWrapper=new QueryWrapper();
        int flag=0;
        //记录未删除的管理员账号
        List<String> ManagerAccountNumbersNotDelete=new ArrayList<>();
        for (String managerAccountNumber : managerAccountNumbers) {
            queryWrapper.clear();
            queryWrapper.eq("manager_account_number",managerAccountNumber);
            int delete = administratorsManagerMapper.delete(queryWrapper);
            if(delete>0){
                flag++;
            }else{
                //记录未删除的管理员账号
                ManagerAccountNumbersNotDelete.add(managerAccountNumber);
            }
        }
        if(flag==managerAccountNumbers.size()){
            result.setCode("200");
            result.setResult(Result.RESULT_FLG.SUCCESS.getValue());
            result.setData(null);
            result.setMsg("全部删除失败");
        }else{
            result.setCode("403");
            result.setResult(Result.RESULT_FLG.FAIL.getValue());
            result.setData(null);
            result.setMsg("删除失败");
            //将未删除成功的账号添加到data中
            result.setData(ManagerAccountNumbersNotDelete);
        }
        return result;
    }

    @Override
    public Result resetManagerPassword(String managerAccountNumber) {
        Result<String> result=new Result<>();
        String managerPassword="66666";

        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("manager_account_number",managerAccountNumber);

        AdministratorsManager manager=new AdministratorsManager();
        manager.setManagerPassword(managerPassword);

        int update = administratorsManagerMapper.update(manager, queryWrapper);
        if(update>0){
            result.setCode("200");
            result.setResult(Result.RESULT_FLG.SUCCESS.getValue());
            result.setData(managerPassword);
            result.setMsg("成功");
        }else{
            result.setCode("403");
            result.setResult(Result.RESULT_FLG.FAIL.getValue());
            result.setData(null);
            result.setMsg("失败");
        }
        return result;
    }

    @Override
    public Result getGenerateManagerAccountNumber() {

        Result<StringBuffer> result=new Result<>();
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.isNotNull("manager_account_number");
        Integer integer = administratorsManagerMapper.selectCount(queryWrapper);
        StringBuffer stringBuffer1 = new StringBuffer("manager");
        StringBuffer stringBuffer2 = StringUtils.codedNumber(integer + 1, 5);
        stringBuffer1.append(stringBuffer2);
        if(integer!=0){
            result.setCode("200");
            result.setResult(Result.RESULT_FLG.SUCCESS.getValue());
            result.setData(stringBuffer1);
            result.setMsg("成功");
        }else{
            result.setCode("403");
            result.setResult(Result.RESULT_FLG.FAIL.getValue());
            result.setData(null);
            result.setMsg("失败");
        }
        return result;
    }


}
